package fr.afcepf.atod12.projet1.dao.implementations;

import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import fr.afcepf.atod12.projet1.dao.interfaces.IDaoEvenement;
import fr.afcepf.atod12.projet1.entities.Evenement;
import fr.afcepf.atod12.projet1.entities.Medecin;
import fr.afcepf.atod12.projet1.entities.Patient;
import fr.afcepf.atod12.projet1.entities.TypeEvenement;

@Service("daoEvenement")
@Transactional 
public class DaoEvenementImpl implements IDaoEvenement {

	@PersistenceContext(unitName="myPersistenceUnit")
	private EntityManager entityManager;
	
	@Override
	public int creationEvenement(Evenement evt) {
		entityManager.persist(evt);
		return evt.getIdEvenement();
	}

	@Override
	public int ModifierEvenement(Evenement evt) {
		entityManager.merge(evt);
		return evt.getIdEvenement();
	}

	@Override
	public void SupprimerEvenement(int idEvenement) {
		entityManager.remove(this.getEvenementById(idEvenement));
	}
		
	@Override
	public Evenement getEvenementById(int idEvenement) {
		return entityManager.find(Evenement.class, idEvenement);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Evenement> getAllEvenementAValider() {
		Query query=entityManager.createQuery("select e from Evenement e " +
		  "where e.statut.idStatut=:idStatut");
		query.setParameter("idStatut", 1);
		return query.getResultList();
	}

	@Override
	public Medecin getMedecinByMedecinWithEvenementsAValider(int idMedecin) {
		Query query=entityManager.createQuery("select m from Medecin m " +
		  "left join fetch m.evenements ev where ev.statut.idStatut=:idStatut and m.idMedecin=:idMedecin");
		query.setParameter("idMedecin", idMedecin);
		query.setParameter("idStatut", 1);

		try {
			return (Medecin) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Evenement> getEvenementByDate(Date date) {
		Query query=entityManager.createQuery("select e from Evenement e " +
		  "where e.dateEvenement=:date");
		query.setParameter("date", date);
		return query.getResultList();
	}

	@Override
	public Medecin getMedecinByDateMedecinWithEvenements(int idMedecin, Date date) {
		
		Query query=entityManager.createQuery("select m from Medecin m " +
		  "left join fetch m.evenements ev where ev.dateEvenement=:date and m.idMedecin=:idMedecin");
		query.setParameter("date", date);
		query.setParameter("idMedecin", idMedecin);
		try{
			return (Medecin) query.getSingleResult();
		}catch(NoResultException e){
			return null;
		}
	}

	@Override
	public Patient getPatientByIdEvt(Integer idEvenement) {

		Query query=entityManager.createQuery("select patient from Evenement e " +
		  " where e.idEvenement=:idEvenement");
		query.setParameter("idEvenement", idEvenement);
		try {
			return (Patient) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TypeEvenement> getAllTypeEvenement() {
		Query query=entityManager.createQuery("from TypeEvenement e ");
		return query.getResultList();
	}

	@Override
	public TypeEvenement getTypeEvenementByName(String name) {
		Query query=entityManager.createQuery("from TypeEvenement te where te.libelleTypeEvenement =:name");
		query.setParameter("name", name);
		try {
			return (TypeEvenement) query.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
	}

}
